package persistence.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import persistence.db.DataSet;
import persistence.db.DatabaseConnector;
import persistence.model.AbstractModel;
import persistence.model.SeanceSpeciale;

public class SeanceSpecialeDAO extends AbstractDAO{
	
	private SeanceSpeciale seanceSpeciale;
	
	/**
	 * Constructeur d'une seance speciale DAO
	 */
	public SeanceSpecialeDAO() {
		setTableName("SEANCE_SPECIALE");
	}

	/**
	 * Constructeur d'une seance speciale DAO
	 * @param seSpe seance speciale associée
	 */
	public SeanceSpecialeDAO(SeanceSpeciale seSpe) {
		seanceSpeciale = seSpe;
		setId(seanceSpeciale.getId());
	}
	
	public DataSet all() {
		return new DataSet(this);
	}

	public AbstractModel makeModel(ResultSet rset) {
		try {
			return new SeanceSpeciale(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getInt(4), rset.getInt(5));
		} catch (SQLException e) {
			System.out.println("makeModel error");
			e.printStackTrace();
		}
		return null;
	}

	public void save() {
		String query;
		ResultSet result;
		query = "INSERT INTO " + getTableName() +
			" VALUES ( S_ID_" + getTableName() + ".nextval, '" +
			seanceSpeciale.getNomSeanceSpeciale() + "', '" +
			seanceSpeciale.getDescriptionSeanceSpeciale() +"', "+
			seanceSpeciale.getIdPeriode() + ", " +
			seanceSpeciale.getIdSalle() + ")";
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
		setId(this.getId()); /* Mise a jour de l'identifiant */
	}

	public void save(int id) {
		String query;
		ResultSet result;
		query = "UPDATE " + getTableName() +
			" SET nomSeanceSpe = '" + seanceSpeciale.getNomSeanceSpeciale() +
			"', descriptionSeanceSpe = '" + seanceSpeciale.getDescriptionSeanceSpeciale() +
			"', fk_seance_spe_periode = " + seanceSpeciale.getIdPeriode() +
			", fk_seance_spe_salle = " + seanceSpeciale.getIdSalle() +
			"' WHERE ID_"+ getTableName() + " = "+ id;
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
	}
	
}
